from fastapi import APIRouter, Query

from pojo.response import Response
from service import dataService

tradeRouter = APIRouter(tags=['backtest'])

@tradeRouter.get('/history/get_tradeHistory',
                    summary='获得交易记录')
async def get_tradeHistory(
        orderID: str = Query(description='订单号')
):
    trade_qs = await dataService.get_tradeHistory(orderID)
    if trade_qs is None:
        return Response(
            code=0,
            message="获取交易记录失败, 订单号不存在!"
        )

    return Response(
        code=1,
        data=trade_qs,
        message='获得交易记录成功!'
    )

@tradeRouter.get('/history/get_orderHistory',
                    summary='获得所有订单记录')
async def get_orderHistory():
    order_qs = await dataService.get_orderHistory()
    if order_qs is None:
        return Response(
            code=0,
            message='订单记录为空!'
        )

    return Response(
        code=1,
        data=order_qs,
        message='获取所有订单记录成功!'
    )

@tradeRouter.delete('/history/delete_history',
                       summary='删除订单记录')
async def delete_history(
        orderID: str = Query(description='订单ID')
):
    if await dataService.delete_history(orderID) is None:
        return Response(
            code=0,
            message="删除订单失败, 订单不存在!"
        )
    else:
        return Response(
            code=1,
            message="删除订单成功!"
        )

@tradeRouter.delete('/history/delete_allHistory',
                       summary='删除所有订单')
async def delete_allHistory():
    await dataService.delete_allHistory()
    return Response(
        code=1,
        message="删除所有订单成功!"
    )